function delete_confirm1(obj){
    if (confirm("Are you sure？")) {
        //alert('Delete successfully.'); //用来测试
        //删除表格一行
        var root = obj.parentNode.parentNode;
        root.parentNode.removeChild(root);
        //更新数据库
        var value = $(obj).parent().parent().find("td");
        var a = value.eq(0).text(); //得到orderId
        $.ajax({
            type: "POST",
            url:"deleteOrder1",
            data:{orderId:a},
            dataType:"json",
            success: function(data){
                alert("Delete successfully!");
                window.location.reload();
                var url = this.href;
                e.preventDefault();
                $('#dynamic-table').reload();
                window.location.href = "/allOrder";
            }
        })
        //显示提醒框
        document.getElementById("remind").style.display='block';
    }

}

function delete_confirm2(obj){
    if (confirm("Are you sure？")) {
        //alert('Delete successfully.'); //用来测试
        //显示提醒框
        document.getElementById("remind").style.display='block';
        //删除表格一行
        var root = obj.parentNode.parentNode;
        root.parentNode.removeChild(root);
        //更新数据库
        var value = $(obj).parent().parent().find("td");
        var a = value.eq(0).text(); //得到orderId
        $.ajax({
            type: "POST",
            url:"deleteOrder2",
            data:{orderId:a},
            dataType:"json",
            success: function(){
                alert("Delete successfully!");
                window.location.reload();
                var url = this.href;
                e.preventDefault();
                $('#dynamic-table').reload();
                window.location.href = "/waitOrder";
            }
        })
        //显示提醒框
        document.getElementById("remind").style.display='block';
    }
    //window.location.reload();
}

function delete_confirm3(obj){
    if (confirm("Are you sure？")) {
        //alert('Delete successfully.'); //用来测试
        //显示提醒框
        document.getElementById("remind").style.display='block';
        //删除表格一行
        var root = obj.parentNode.parentNode;
        root.parentNode.removeChild(root);
        //更新数据库
        var value = $(obj).parent().parent().find("td");
        var a = value.eq(0).text(); //得到orderId
        $.ajax({
            type: "POST",
            url:"deleteOrder3",
            data:{orderId:a},
            dataType:"json",
            success: function(){
                alert("Delete successfully!");
                window.location.reload();
                var url = this.href;
                e.preventDefault();
                $('#dynamic-table').reload();
                window.location.href = "/deliveredOrder";
            }
        })
        //显示提醒框
        document.getElementById("remind").style.display='block';
    }
    //window.location.reload();
}



var a;
function edit(val){
    //编辑弹框里输入框的默认值
    var value = $(val).parent().parent().find("td");
    document.getElementById("modal_orderId").value=value.eq(0).text();
    document.getElementById("modal_buyerName").value=value.eq(1).text();
    document.getElementById("modal_shipAddress").value=value.eq(2).text();
    document.getElementById("modal_billAddress").value=value.eq(3).text();
    document.getElementById("modal_cardType").value=value.eq(4).text();
    document.getElementById("modal_creditCard").value=value.eq(5).text();
    document.getElementById("modal_totalPrice").value=value.eq(6).text();
    document.getElementById("modal_status").value=value.eq(7).text();

    a=value;
}
//订单修改提交
function apply(){
    //var value = $(a).parent().parent().find("td");
    a.eq(0).text=document.getElementById("modal_orderId").value;
    a.eq(1).text=document.getElementById("modal_buyerName").value;
    a.eq(2).text=document.getElementById("modal_shipAddress").value;
    a.eq(3).text=document.getElementById("modal_billAddress").value;
    a.eq(4).text=document.getElementById("modal_cardType").value;
    a.eq(5).text=document.getElementById("modal_creditCard").value;
    a.eq(6).text=document.getElementById("modal_totalPrice").value;
    a.eq(7).text=document.getElementById("modal_status").value;
    var order={
        "orderId" : document.getElementById("modal_orderId").value, //得到userId
        "username" : document.getElementById("modal_buyerName").value,
        "shipAddress1" : document.getElementById("modal_shipAddress").value,
        "billAddress1" : document.getElementById("modal_billAddress").value,
        "cardType" : document.getElementById("modal_cardType").value,
        "creditCard" : document.getElementById("modal_creditCard").value,
        "status" : document.getElementById("modal_status").value
    };
    $.ajax({
        type: "POST",
        contentType:"application/json",
        url:"editOrder",
        dataType: "json",
        data:JSON.stringify(order),
        success: function(data){
            window.location.reload();
            var url = this.href;
            e.preventDefault();
            $('#dynamic-table').reload();

        }
    })
    //显示提醒框
    document.getElementById("remind4").style.display='block';

}

function showsY(){
    document.getElementById("modal_status").value = "Y";
}

function showsN(){
    document.getElementById("modal_status").value = "N";
}
var b={};
function search_edit(val){
    //编辑弹框里输入框的默认值
    var value = $(val).parent().parent().find("td");
    document.getElementById("search_orderId").value=value.eq(0).text();
    document.getElementById("search_userName").value=value.eq(1).text();
    document.getElementById("search_shipAddress").value=value.eq(2).text();
    document.getElementById("search_billAddress").value=value.eq(3).text();
    document.getElementById("search_cardType").value=value.eq(4).text();
    document.getElementById("search_creditCard").value=value.eq(5).text();
    document.getElementById("search_totalPrice").value=value.eq(6).text();
    document.getElementById("search_status").value=value.eq(7).text();
}


function shows(obj) {
    document.getElementById("search_status").value = obj.innerHTML;
}

//订单修改提交
function search_apply(){
    var value = $(b).parent().parent().find("td");
    value.eq(0).text=document.getElementById("search_orderId").value;
    value.eq(1).text=document.getElementById("search_userName").value;
    value.eq(2).text=document.getElementById("search_shipAddress").value;
    value.eq(3).text=document.getElementById("search_billAddress").value;
    value.eq(4).text=document.getElementById("search_cardType").value;
    value.eq(5).text=document.getElementById("search_creditCard").value;
    value.eq(6).text=document.getElementById("search_totalPrice").value;
    value.eq(7).text=document.getElementById("search_status").value;

    var order={};
    order["orderId"] = document.getElementById("search_orderId").value; //得到userId
    order["userName"] =document.getElementById("search_buyerName").value;
    order["shipAddress1"] = document.getElementById("search_shipAddress").value;
    order["billAddress1"] =document.getElementById("search_billAddress").value;
    order["cardType"]=document.getElementById("search_cardType").value;
    order["creditCard"]=document.getElementById("search_creditCard").value;
    order["totalPrice"] =document.getElementById("search_totalPrice").value;
    order["status"] =document.getElementById("search_status").value;
    $.ajax({
        type: 'POST',
        contentType:"application/json",
        url:"/order/searchEditOrder",
        dataType: 'json',
        data:JSON.stringify(order),
        success: function(){
            alert("Edit successfully!");
        }
    })
    //window.location.reload();
}

//订单发货
function toDeliver(obj){
    //从代发货订单表格中删除该订单
    if (confirm("Do you want to deliver this order?")) {
        //document.getElementById("remind3").style.display = 'block';
        var root = obj.parentNode.parentNode;
        root.parentNode.removeChild(root);
        //AJAX更新数据库
        var value = $(obj).parent().parent().find("td");
        var order = {
            "orderId": value.eq(0).text(),
            "userName": value.eq(1).text(),
            "shipAddress1": value.eq(2).text(),
            "billAddress1": value.eq(3).text(),
            "cardType" : value.eq(4).text(),
            "creditCard": value.eq(5).text(),
            "totalPrice": value.eq(6).text(),
            "status": "Y"
        };
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "toDeliver",
            dataType: 'json',
            data: JSON.stringify(order),
            success: function (data) {
                alert("Deliver successfully!");
                window.location.reload();
            }
        })
    }

}

//订单查询显示搜索结果
function showSearch(){
    var s = $("#searchContent").val(); //搜索框的内容
    $.ajax({
        type: "get",
        contentType:"application/json",
        url:"searchOrder",
        dataType: "json",
        cache:false,
        data:{searchContent:s},
        success: function(data){
            var table = document.getElementById("modal-table-table");
            var t_body = document.getElementById("modal-table-body");

            for(var i = 0;i<data.length;i++){
                var tr_order = document.createElement("tr");
                var td_scOrderId = document.createElement("td");
                var td_scBuyerName = document.createElement("td");
                var td_scShipAddress = document.createElement("td");
                var td_scBillAddress = document.createElement("td");
                var td_scCardType = document.createElement("td");
                var td_scCreditCard = document.createElement("td");
                var td_scTotalPrice = document.createElement("td");
                var td_scStatus = document.createElement("td");
                var td_scOperate = document.createElement("td");

                td_scOperate.innerHTML ="<button type=\"button\" class=\"btn btn-link\" data-toggle=\"modal\" data-target=\"#myModal\" onclick=\"edit(this)\">Edit</button>"
                +"<button type=\"button\" class=\"btn btn-link\" onclick=\"delete_confirm1(this)\">Delete</button>";
                //var bt = document.createElement("button");
                //bt.innerHTML = 'Edit';
                //bt.onclick = search_edit(this);

                //var bt2 = document.createElement("button");
                //bt2.innerHTML = 'Delete';
                //bt2.onclick = delete_confirm(this);

                td_scOrderId.innerHTML =data[i].orderId;
                td_scBuyerName.innerHTML = data[i].username;
                td_scShipAddress.innerHTML = data[i].shipAddress1;
                td_scBillAddress.innerHTML = data[i].billAddress1;
                td_scCardType.innerHTML = data[i].cardType;
                td_scCreditCard.innerHTML = data[i].creditCard;
                td_scTotalPrice.innerHTML = data[i].totalPrice;
                td_scStatus.innerHTML = data[i].status;
                tr_order.appendChild(td_scOrderId);
                tr_order.appendChild(td_scBuyerName);
                tr_order.appendChild(td_scShipAddress);
                tr_order.appendChild(td_scBillAddress);
                tr_order.appendChild(td_scCardType);
                tr_order.appendChild(td_scCreditCard);
                tr_order.appendChild(td_scTotalPrice);
                tr_order.appendChild(td_scStatus);
                tr_order.appendChild(td_scOperate);
                t_body.appendChild(tr_order);
            }
            table.appendChild(t_body);
        },
        error:function(err){
            alert("发生错误");
        }
    });
    document.getElementById("myModal2").style.display='block';
}

/*
$(document).ready(function (){
    $('#table1').dataTable();
});

$.ajax({
    type:'get',
    url:'/order/searchOrder',
    data:{

    },
    dataType:'text', //服务器返回的数据类型
    success:function (msg){
        var msgObj = JSON.parse(msg);
        //重新构建table1
        $('#table1').dataTable().fnClearTable(); //清楚表格内容，保留表头
        $('#table1').dataTable().fnAddData(packagingdatatabledata(msgObj),true); //数据必须是json对象或Json对象数组

    },
    error:function (){
        alert('error');
    }

})

//将后端返回的数据转成datatable须要的格式
function packagingdatatabledata(msgObj){
    var editHtml="<button type='button' class='btn btn-link' data-toggle='modal' data-target='#myModal' onclick='edit(this)'>Edit</button>\n" +
                    " <button type='button' class='btn btn-link' onclick='delete_confirm(this)'>Delete</button>"
    var a=[];
    var tableName =['orderId','userName','orderDate',,'shipAddress','billAddress',,'creditCard','totalPrice','status','operate'];

}
*/
